app.controller('channelUserController',function ($scope, $controller, $location, channelUserService, uploadService) {

    $controller('baseController',{$scope:$scope});

    $scope.searchEntity = {};

    $scope.entity = {contractUrl:null, stepRateChannelList: [{prefix:0,suffix:0,personalTax:0,valueAddedTax:0,laborServiceFee:0}]};

    //分页获取数据
    $scope.findPage = function (pageNum, pageSize) {

        if (pageNum == null)
            pageNum = 1;

        if (pageSize == null)
            pageSize = 10;

        channelUserService.findPage(pageNum,pageSize,$scope.searchEntity).success(function (response) {

            if (response.code == 200){
                $scope.channelUserList = response.data;

                //更新总记录数
                $scope.paginationConf.totalItems=response.total;

            }else {
                alert(response.msg);
            }

        });

    };



    //添加或更新
    $scope.insertOrUpdate = function () {

        console.log($scope.isClick);
        if ($scope.isClick){
            console.log("重复提交");
            return;
        }
        //标记为点击状态
        $scope.isClick = true;

        if (signTime != null)
            $scope.entity.signTime = new Date(Date.parse(signTime.replace(/-/g, "/")));

        var isError = false;

        if ($scope.isNull($scope.entity.type)) {

            alert("未选择渠道类型");

            isError = true;
        }else {

            if ($scope.isNull($scope.entity.name)) {

                isError = true;

                if ($scope.entity.type == '个人'){

                    alert("未设置姓名");

                } else if ($scope.entity.type == '公司') {

                    alert("未设置公司名称");

                }
            }

            if ($scope.isNull($scope.entity.taxNo)){

                if ($scope.entity.type == '公司') {

                    isError = true;

                    alert("未设置公司税号");

                }
            }

            if ($scope.isNull($scope.entity.gender)){

                if ($scope.entity.type == '个人') {

                    isError = true;

                    alert("未设置性别");

                }
            }

        }


        if ($scope.isNull($scope.entity.phone)) {
            alert("未设置手机号");

            isError = true;
        }

        if ($scope.isNull($scope.entity.channelRate)) {
            alert("未设置渠道费率");

            isError = true;
        }

        if ($scope.isNull($scope.entity.channelRateTwo)) {
            alert("未设置渠道二档费率");

            isError = true;
        }

        if ($scope.isNull($scope.entity.signStatus)) {

            alert("未选择签约状态");

            isError = true;
        }else {

            if ($scope.entity.signStatus == 1) {//已签约状态

                if ($scope.isNull($scope.entity.signTime)){
                    alert("未选择签约时间");

                    isError = true;
                }

            }else {//未签约状态

                $scope.entity.signTime = null;
            }

        }

        var stepRateChannelList = $scope.entity.stepRateChannelList;

        if (stepRateChannelList == null || stepRateChannelList.length == 0){
            alert("未设置阶梯费率");
            isError = true;
        }

        for (var i = 0; i < stepRateChannelList.length; i++) {

            if (i == 0){

                if (stepRateChannelList[i].prefix == null ||  stepRateChannelList[i].prefix < 0) {
                    alert("第 "+(i + 1)+" 阶梯前缀错误");

                    isError = true;
                    break;
                }

            }

            if (i != 0){

                if (stepRateChannelList[i].prefix == null ||  stepRateChannelList[i].prefix <= 0) {
                    alert("第 "+(i + 1)+" 阶梯前缀错误");

                    isError = true;
                    break;
                }

            }

            if (stepRateChannelList[i].suffix == null ||  stepRateChannelList[i].suffix <= 0) {
                alert("第 "+(i + 1)+" 阶梯后缀错误");

                isError = true;
                break;
            }


        }



        if (isError){
            //还原按钮初始状态
            $scope.isClick = false;

            return;

        }


        var objService = null;

        if ($scope.entity.id == null){
            //新增
            objService = channelUserService.insert($scope.entity);
        } else {
            objService = channelUserService.update($scope.entity);
        }


        //获取结果
        objService.success(function (response) {

            if (response.code == 200){

                $scope.entity = {};

                location.href = 'channelUser-manage.html';

            } else {
                alert(response.msg);
            }

            //还原按钮初始状态
            $scope.isClick = false;
        }).error(function () {
            //还原按钮初始状态
            $scope.isClick = false;
        });
    };

    $scope.prompt = "仅限 pdf 格式";

    //上传合同
    $scope.uploadContract = function () {

        console.log("合同上传执行");

        uploadService.uploadChannelUserContract().success(function (response) {

            if (response.code == 200){

                $scope.entity.contractUrl = response.data;

                $scope.prompt = response.msg;

            } else {
                alert(response.msg);
            }

        });

    };

    //根据id获取渠道信息
    $scope.findOne = function () {

        //获取渠道id
        var channelUserId = $location.search()['channelUserId'];

        if (channelUserId == null)
            return;

        channelUserService.findOne(channelUserId).success(function (response) {

            if (response.code == 200){
                $scope.entity = response.data;

                if ($scope.entity.signTime != null)//签约时间回显
                    setSignTimeTime($scope.myFormatDate($scope.entity.signTime,"yyyy-MM-dd HH:mm:ss"))
                var stepRateChannelList = $scope.entity.stepRateChannelList;

                if (stepRateChannelList == null || stepRateChannelList.length == 0){

                    $scope.entity.stepRateChannelList = [{prefix:0,suffix:0,personalTax:0,valueAddedTax:0,laborServiceFee:0}];

                } else {

                    for (var i = 0; i < stepRateChannelList.length; i++) {
                        console.log(stepRateChannelList[i].prefix/10000)
                        //前缀
                        stepRateChannelList[i].prefix = stepRateChannelList[i].prefix/10000;

                        //后缀
                        stepRateChannelList[i].suffix = stepRateChannelList[i].suffix/10000;

                        //服务费
                        stepRateChannelList[i].laborServiceFee = (stepRateChannelList[i].laborServiceFee * 100).toFixed(2);


                    }

                }
            } else {
                alert(response.msg);
            }

        });

    };


    //获取渠道下拉框集合
    $scope.findChannelUserNames = function () {

        //获取渠道id
        var channelUserId = $location.search()['channelUserId'];


        channelUserService.findChannelUserNames(channelUserId).success(function (response) {

            if (response.code == 200){

                $scope.channelUserNames = response.data;

            } else {
                alert(response.msg)
            }

        })

    };



    //重置查询参数
    $scope.resetSearchEntity = function () {

        $scope.searchEntity = {};

        $scope.reloadList();
    };

    //标记阶梯数
    $scope.indexStepRateChannelList = ["StepRate"];


    //添加阶梯级数
    $scope.updateIndexStepRateChannelList = function(){
        console.log("添加执行了");

        $scope.indexStepRateChannelList.push("StepRate");

    };


    //增加阶梯级数
    $scope.createStepList = function(){


        var stepRate = {prefix:0,suffix:0,personalTax:0,valueAddedTax:0,laborServiceFee:0};

        var addLength = $scope.indexStepRateChannelList.length - $scope.entity.stepRateChannelList.length;


        for (var j = 0; j < addLength; j++) {//循环选中规格的结果集


            $scope.entity.stepRateChannelList.push(stepRate);


        }

    };

    //删除阶梯费率的一行
    $scope.deletestepRateRow = function (index) {

        if ($scope.entity.stepRateChannelList.length == 1)
            return;

        $scope.entity.stepRateChannelList.splice(index,1);

        $scope.indexStepRateChannelList.splice(index,1);
    };

});